﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.baidu.ai;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;


public partial class User_Face_Signup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text= "<span style='color: #c2c2c2; font-size: x-small'>**请在光线充足的地方录入人脸以提高刷脸登录准确度！**<br />**准备好后点击下方按钮即可录入人脸！**</ span > ";
    }

    protected void capture_Click(object sender, EventArgs e)
    {
        string name = Session["UserName"].ToString();
        string tel = Session["UserTel"].ToString();
        string password = Session["UserPass"].ToString();
        string email = Session["UserMail"].ToString();

        string PreBase64 = HiddenField1.Value;
        string Base64 = "";
        if (PreBase64.Contains("data:image/png;base64,"))
        {
            Base64 = PreBase64.Replace("data:image/png;base64,", "");
        }
        if(PreBase64.Contains("data:image/jpg; base64,"))
        {
            Base64 = PreBase64.Replace("data:image/jpg;base64,", "");
        }
        if (PreBase64.Contains("data:image/jpeg; base64,"))
        {
            Base64 = PreBase64.Replace("data:image/jpeg;base64,", "");
        }
        //PreBase64.Replace("data:image/png;base64,", "");
        //Response.Write(Base64);
        string Access_Token = AccessToken.getAccessToken();
        //string feedback = FaceSearch.Search(Access_Token, Base64);
        string Face_Add_Return = FaceAdd.add(Access_Token, Base64, tel);
        //Response.Write(Face_Add_Return);
        int Face_Add_Result = ReturnResultDetect.GetFaceAddReturnResult(Face_Add_Return);
        //Response.Write(Face_Add_Result);
        if (Face_Add_Result==1)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringProManage"].ConnectionString;
            try
            {
                conn.Open();
                string SQL = "insert into Users (uername,password,usertype,name,tel,email) values (@tel,@password,2,@name,@tel,@email)";

                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL;

                //依次添加每个字段的值
                SqlParameter ptel = new SqlParameter("@tel", SqlDbType.VarChar, 11);
                ptel.Value = tel;
                cmd.Parameters.Add(ptel);
                SqlParameter ppassword = new SqlParameter("@password", SqlDbType.VarChar, 20);
                ppassword.Value = password;
                cmd.Parameters.Add(ppassword);
                SqlParameter pname = new SqlParameter("@name", SqlDbType.NVarChar, 10);
                pname.Value = name;
                cmd.Parameters.Add(pname);
                SqlParameter pemail = new SqlParameter("@email", SqlDbType.NVarChar, 30);
                pemail.Value = email;
                cmd.Parameters.Add(pemail);

                int R = cmd.ExecuteNonQuery();  //R 的值就是返回影响表的行数
                if (R > 0)
                {
                    Session["ReturnPath"] = "Move_login.aspx";
                    Session["Message"] = "恭喜，注册成功！";
                    Response.Redirect("Success.aspx");
                }
                else
                {
                    Session["Message"] = "插入失败，请检查您的操作！";
                    Response.Redirect("Error.aspx");
                }
                //Session["ReturnPath"] = "Admin_Add.aspx";
                //Response.Redirect("Mid.aspx");
            }
            finally
            {
                conn.Close();
            }
        }
        else
        {
            Label1.Text = "<span style='color: #f00; font-size: smaller'>**未检测到人脸，请重试！**</span> ";
        }
        

        
    }
}